use "${datadir}\HTB_repayment", replace
capture drop _merge
merge 1:1 schemeid using ///
	"${datadir}\HCA_redemptions", keep(1 3)
merge 1:1 schemeid using ///
	"${datadir}\HCA_redemptions_sale", ///
	keep(1 3) gen(merge_sale) 
	


	
*--- Which subsample
keep if merge_sale != 3
gen repay 	= 1 if _merge==3 
replace repay 	= 3 if missing(repay)

global repaysample _stair

*--- Collapse
preserve
	collapse_vars repay
	save "${tabledir}\descriptive_mean_repay${repaysample}", replace
restore

preserve
	collapse_vars_count repay
	rename v1 v1_count
	rename v2 v2_count
	save "${tabledir}\descriptive_count_repay${repaysample}", replace
restore


*--- Put together the table
collapse_vars_sd repay

gen sort_order = _n		
merge 1:1 _varname using ///
	"${tabledir}\descriptive_mean_repay_sold", nogen keep(3)
sort sort_order		
drop sort_order

global tabsample _repay${repaysample}	// kludge to make table_diff work
table_diff repay

prepare_ttest_result
drop *_sd

table_diff_format 
qui: latexTable


outsheet using "${tabledir}\descriptive_diff_repay${repaysample}_1.tex" ///
	if regexm(_varname, "Age|First time|quad Gross income"), replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff_repay${repaysample}_2.tex" ///
	if regexm(_varname,"value|Down|Equity|Interest|Maturity|fixed"), ///
	replace noquote nonames
	
outsheet using "${tabledir}\descriptive_diff_repay${repaysample}_3.tex" ///
	if regexm(_varname, "LTV|LTI|Payment"), replace noquote nonames
	
	
// Last row of the table with n obs
use "${tabledir}\descriptive_count_repay${repaysample}", replace
merge 1:1 _varname using  ///
	"${tabledir}\descriptive_count_repay_sold", keep(3) nogen

	keep in 2
	replace _varname = "\$N\$"
	gen totsum = v2 + v1 
	order _varname v0   v1 v2  totsum
	format v2 v1 v0 totsum %7.0fc
qui: latexTable
outsheet using "${tabledir}\num_diff_repay${repaysample}.tex", ///
	replace noquote nonames


